package com.example.service1.provider.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.example.service1.common.api.entity.ResponseResult;
import com.example.service1.common.dto.FileTestDto;
import com.example.service1.common.dto.TestDto;
import com.example.service1.common.vo.TestVo;
import com.example.service1.provider.fallback.Service1Fallback;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;

@RestController("Service1Controller")
@RequestMapping("/service1")
public class Service1Controller {

    /**
     * 由于使用实现类方式实现了该方法服务熔断降级方法，所以这里不指定了
     */
    @SentinelResource(value = "/service1/get",
            blockHandler = "noArgGetBlockException", blockHandlerClass = Service1Fallback.class,
            fallback = "noArgGetThrowable",fallbackClass = Service1Fallback.class)
    @GetMapping("/get/noArg")
    public ResponseResult<TestVo> noArgGet(){
        long round = Math.round(Math.random() * 5);
        System.out.println(round);
        if(round==0 || round == 1){
            int i=10/0;
        }
        return ResponseResult.success(TestVo.defaultCreate());
    }

    @SentinelResource(value = "/service1/get",
            fallback = "pathArgGetThrowable",fallbackClass = Service1Fallback.class)
    @GetMapping("/get/pathArg/{name}/{date}")
    public ResponseResult<TestVo> pathArgGet(@PathVariable("name")String name, @PathVariable("date")LocalDate date){
        if(name.equals("触发fallback")){
            int i=10/0;
        }
        return ResponseResult.success(TestVo.defaultCreate().setName(name).setLocalDate(date));
    }

    @PostMapping(value = "/post/pathAndBodyArg/{name}/{date}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<TestVo> pathAndBodyArgPost(@PathVariable("name")String name, @PathVariable("date")LocalDate date,
                                 @RequestBody TestDto dto){
        int i =10/0;
        TestVo testVo = new TestVo();
        testVo.setName(name)
                .setLocalDate(date)
                .setAge(Math.toIntExact(dto.getAge()))
                .setList(dto.getList());
        return ResponseResult.success(testVo);
    }

    @PutMapping("/put/pathAndBodyArg/{name}/{date}")
    public ResponseResult<TestVo> pathAndBodyArgPut(@PathVariable("name")String name, @PathVariable("date")LocalDate date,
                                    @RequestBody TestDto dto){
        long round = Math.round(Math.random() * 5);
        System.out.println(round);
        if(round==0 || round == 1){
            int i=10/0;
        }
        TestVo testVo = new TestVo();
        testVo.setName(name)
                .setLocalDate(date)
                .setAge(Math.toIntExact(dto.getAge()))
                .setList(dto.getList());
        return ResponseResult.success(testVo);
    }

    @PostMapping("/post/file/{e}")
    public ResponseResult<TestVo> pathAndBodyArgAndFileArgPost(@PathVariable("e") String e,
                                                               FileTestDto fileTestDto){
        TestVo testVo = new TestVo();
        if(fileTestDto.getMultipartFile()!=null){
            testVo.setName(fileTestDto.getMultipartFile().getName());
            System.out.println();
        }
        if(fileTestDto.getFileList()!=null){
            for(MultipartFile file:fileTestDto.getFileList()){
                testVo.getList().add(file.getName());
            }
        }
        testVo.setLocalDate(fileTestDto.getLocalDate())
                .setAge(Math.toIntExact(fileTestDto.getAge()))
                .setList(fileTestDto.getList());
        testVo.getList().add(e);
        return ResponseResult.success(testVo);
    }


}
